//
//  BaseViewController.m
//  Divvy-Do
//
//  Created by Sinbad Fly on 5/30/12.
//  Copyright (c) 2012 ABOM. All rights reserved.
//

#import "BaseViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Utility.h"

@implementation BaseViewController

@synthesize superViewController = _superViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];    
}

#pragma Private Method

- (void)applyBaseAppearance
{
    UINavigationBar *navBar = self.navigationController.navigationBar;
    UIView *v0 = [[navBar subviews] objectAtIndex:0];          
    
    navBar.tintColor = [UIColor whiteColor];
    v0.backgroundColor = [UIColor whiteColor];
    
    v0.layer.shadowColor = [[UIColor colorWithRed:209./255 green:209./255 blue:209./255 alpha:1] CGColor];
    v0.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    v0.layer.shadowRadius = 1.0f;
    v0.layer.shadowOpacity = 1; 
    
    
    [self.view.superview setBackgroundColor:[UIColor whiteColor]];
    
    [v0 setNeedsDisplay];     
}

#pragma Public Method

- (void)setTitle:(NSString *)aTitle
{
    UILabel *titleUI = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
    UIView *viewTitle = [[[UIView alloc] init] autorelease]; 
    CGFloat wt = 0;
    
    viewTitle.backgroundColor = [UIColor clearColor];
    
    titleUI.backgroundColor = [UIColor clearColor];
    titleUI.font = JGO_FONT_ARIAL_B(28);
    titleUI.textColor = JGO_TEXT_COLOR_1;
    titleUI.textAlignment = UITextAlignmentCenter;
    titleUI.text = aTitle;
    
    wt = [JGoUtility widthFromString:aTitle font:titleUI.font];
    
    if (wt > 160) {
        wt = 160;
    }
    
    CGRect titleRect = CGRectMake(0, 0, 
                                  wt, 
                                  44);
    viewTitle.frame = titleRect;    
    
    titleRect.origin.y = 0;
    titleRect.size.height = 44;
    titleUI.frame = titleRect;
    
    [viewTitle addSubview:titleUI];
    
    self.navigationItem.titleView = viewTitle;
}



- (void)setBackButtonImageActiveName:(NSString*)imageActiveName 
                        inactiveName:(NSString*)imageInactiveName 
                              target:(id)target action:(SEL)action
{
    [self.navigationItem setHidesBackButton:YES];
        
    UIButton *tmpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *normalImage = [UIImage imageNamed:imageInactiveName];
    UIImage *pressImage = [UIImage imageNamed: imageActiveName];
    
    CGSize size = normalImage.size;
    
    [tmpButton setFrame:CGRectMake(10, (44 - size.height)/2, size.width, size.height)];
    [tmpButton setTitle:@"" forState:UIControlStateNormal];    
    [tmpButton setBackgroundImage:normalImage forState:UIControlStateNormal];  
    [tmpButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];          
    [tmpButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
	[self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tmpButton] autorelease]];     
}

- (void)setLeftButtonImageActiveName:(NSString*)imageActiveName 
                        inactiveName:(NSString*)imageInactiveName 
                              target:(id)target action:(SEL)action
{
    UIButton *tmpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *normalImage = [UIImage imageNamed:imageInactiveName];
    UIImage *pressImage = [UIImage imageNamed: imageActiveName];
    
    CGSize size = normalImage.size;
    
    [tmpButton setFrame:CGRectMake(10, (44 - size.height)/2, size.width, size.height)];
    [tmpButton setTitle:@"" forState:UIControlStateNormal];    
    [tmpButton setBackgroundImage:normalImage forState:UIControlStateNormal];  
    [tmpButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];          
    [tmpButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
	[self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tmpButton] autorelease]];    
}

- (void)setRightButtonImageActiveName:(NSString*)imageActiveName 
                        inactiveName:(NSString*)imageInactiveName 
                              target:(id)target action:(SEL)action
{
    UIButton *tmpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *normalImage = [UIImage imageNamed:imageInactiveName];
    UIImage *pressImage = [UIImage imageNamed: imageActiveName];
    
    CGSize size = normalImage.size;
    
    [tmpButton setFrame:CGRectMake(10, (44 - size.height)/2, size.width, size.height)];
    [tmpButton setTitle:@"" forState:UIControlStateNormal];    
    [tmpButton setBackgroundImage:normalImage forState:UIControlStateNormal];  
    [tmpButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];          
    [tmpButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
	[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tmpButton] autorelease]];    
}

#pragma mark - View lifecycle

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:NO];    
    [self applyBaseAppearance];
}

- (void)viewDidLoad
{
    [super viewDidLoad];    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
    [self setSuperViewController:nil];
    [super dealloc];
}

@end

@implementation UINavigationBar (UINavigationOverride)

- (void)drawRect:(CGRect)rect
{	
    UIImage *theNavigationImage = [UIImage imageNamed:@"titlebar.png"];
    [theNavigationImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];	
}

@end

